package com.scvtc.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.scvtc.domain.enums.AppHttpCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description: 统一响应
 * @Author: yiFei
 * @date: 2022/2/11 23:47
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResResult<T> implements Serializable {

    private static final long serialVersionUID = 7432497328490231576L;
    private Integer code;
    private String msg;
    private T data;

    public ResResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResResult(Integer code) {
        this.code = code;
    }

    /**
     * 响应成功信息
     *
     * @return
     */
    public static ResResult<?> success() {
        return new ResResult<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 携带消息返回成功信息
     *
     * @param data
     * @return
     */
    public static <T> ResResult<T> success(T data) {
        return new ResResult<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 无消息 ( 成功 ) 返回
     *
     * @param data
     * @return
     */
    public static <T> ResResult<T> noMsgSuccess(T data) {
        return new ResResult<>(AppHttpCodeEnum.SUCCESS.getCode(), data);
    }

    /**
     * 无消息 ( 成功 ) 返回
     *
     * @return
     */
    public static <T> ResResult<T> noMsgSuccess() {
        return new ResResult<>(AppHttpCodeEnum.SUCCESS.getCode());
    }

    /**
     * 自定义
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResResult<T> self(Integer code, String msg) {
        return new ResResult<>(code, msg);
    }

    /**
     * 自定义无消息返回
     *
     * @return
     */
    public static <T> ResResult<T> noMsgCustomize(AppHttpCodeEnum appHttpCodeEnum, T data) {
        return new ResResult<>(appHttpCodeEnum.getCode(), data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static ResResult<?> fail() {
        return new ResResult<>(AppHttpCodeEnum.FAIL.getCode(), AppHttpCodeEnum.FAIL.getMsg());
    }

    /**
     * 携带消息返回成功信息
     *
     * @param data
     * @return
     */
    public static <T> ResResult<T> fail(T data) {
        return new ResResult<>(AppHttpCodeEnum.FAIL.getCode(), AppHttpCodeEnum.FAIL.getMsg(), data);
    }

    /**
     * 自定义返回
     */
    public static <T> ResResult<T> customize(AppHttpCodeEnum appHttpCodeEnum) {
        return new ResResult<>(appHttpCodeEnum.getCode(), appHttpCodeEnum.getMsg());
    }

    /**
     * 自定义返回
     */
    public static <T> ResResult<T> customize(AppHttpCodeEnum appHttpCodeEnum, T data) {
        return new ResResult<>(appHttpCodeEnum.getCode(), appHttpCodeEnum.getMsg(), data);
    }


}
